(function(){
		var pass= new Array();
		var ispass=1;
		var $err=$("<span class='tips'></span>");
		$.fn.tgTextValidation=function(p){
			var vStr=$(this).attr("validation");
			var reg = new RegExp();
			var $this=$(this);

			var infostr=$this.attr("title");
			
			if(p){
				var i=getIndex(p,$this);
			}
			
			function insertErr(str,obj){
				if(infostr){
					$err.clone().html(infostr).insertAfter(obj);
				}else{
					$err.clone().html(str).insertAfter(obj);
				}	
			}
			
			$this.focusout(function(){
				$this.next("span").remove();
				if(vStr.indexOf("-")>=0){
					if(vStr.split("-")[0]=="l"){
						var l=parseInt(vStr.split("-")[1]);
						if(vStr.indexOf("nb")>=0){
							if($this.val().replace(/(^\s*)|(\s*$)/g,"")==""){
								insertErr("内容不能为空",$this);
								pass[i]=0;
							}else{
								cl($this.val(),l,i);
							}
						}else{
							cl($this.val(),l,i);
						}							
					}else if(vStr.split("-")[1]=="nb"){
						if($this.val().replace(/(^\s*)|(\s*$)/g,"")==""){
							insertErr("内容不能为空",$this);
							pass[i]=0;
						}else{
							valiStr(vStr.split("-")[0],i);
						}
					}	
				}else{
					valiStr(vStr,i);
				}
			});
			
			//验证长度
			function cl(str,length,i){
				if(str.length>length){
					insertErr("不得多于"+vStr.split("-")[1]+"个字",$this);
					pass[i]=0;							
				}else{
					$this.next("span").remove();
					pass[i]=1;
				}
			}
			//验证字符串
			function valiStr(string,i){
				switch(string)
					{					
							case "zh":
							reg=new RegExp("[u4e00-u9fa5]");
							if(reg.test($this.val())|| $this.val().replace(/(^\s*)|(\s*$)/g,"")==""){
								insertErr("请输入中文",$this);
								pass[i]=0;
							}else{
								$this.next("span").remove();
								pass[i]=1;
							}				
							break;
							
							case "en":
							reg= new RegExp("^[A-Za-z]+$");
							if(!reg.test($this.val())){
								insertErr("请输入英文",$this);
								pass[i]=0;
							}else{
								$this.next("span").remove();
								pass[i]=1;
							}
							break;
							
							case "code":
							reg = new RegExp("^[A-Za-z0-9]+$");
							if(!reg.test($this.val())){
								insertErr("请输入字母或数字",$this);
								pass[i]=0;
							}else{
								$this.next("span").remove();
								pass[i]=1;
							}
							break;
							
							case "email":
							reg= new RegExp("^([a-zA-Z0-9_\.])+@([a-zA-Z0-9_])+((\.[a-zA-Z0-9_]{2,}){1,2})$");
							if(!reg.test($this.val())){
								insertErr("请输入正确的电子邮箱地址",$this);
								pass[i]=0;
							}else{
								$this.next("span").remove();
								pass[i]=1;
							}
							break;
							
							case "pemail":
							reg= new RegExp("^([a-zA-Z0-9_\\.])+@([a-zA-Z0-9_])+((\\.[a-zA-Z0-9_]{2,}){1,2})$");
							if(!reg.test($this.val())){
								insertErr("请输入正确的电子邮箱地址",$this);
								pass[i]=0;
							}else{
								pass[i]=1;
								$.ajax({
	   								type: "post",
	   								url: "/servicer/unique.htm",
	   								cache: false,
	   								data: {type:"person",name:"person_email",value:$this.val()},
	   								success: function(msg){
	   									$this.next("span").remove();
	   									pass[i]=msg;
	   									if(msg==0){
											insertErr("该邮箱已被注册",$this);
											$this.focus();
										}else{
											$this.next("span").remove();
										}
	   								}
								});
							}
							break;
							
							case "cemail":
							reg= new RegExp("^([a-zA-Z0-9_\.])+@([a-zA-Z0-9_])+((\.[a-zA-Z0-9_]{2,}){1,2})$");
							if(!reg.test($this.val())){
								insertErr("请输入正确的电子邮箱地址",$this);
								pass[i]=0;
							}else{
								pass[i]=1;
								$.ajax({
	   								type: "post",
	   								url: "/servicer/unique.htm",
	   								cache: false,
	   								data: {type:"company",name:"comp_email",value:$this.val()},
	   								success: function(msg){
	   									$this.next("span").remove();
	   									pass[i]=msg;
	   									if(msg==0){
											insertErr("该邮箱已被注册",$this);
											$this.focus();
										}else{
											$this.next("span").remove();
										}
	   								}
								});
							}
							break;
							
							case "url":
							reg=new RegExp("((^http)|(^https)):\/\/(\\w)+\.(\\w)+");
							if(!reg.test($this.val())){
								insertErr("请输入url",$this);
								pass[i]=0;
							}else{
								$this.next("span").remove();
								pass[i]=1;
							}
							break;
							
							case "id":
							reg= new RegExp("^[0-9]{17}[0-9xX]$|^[0-9]{14}[0-9xX]$");
							if(!reg.test($this.val())|| $this.val().replace(/(^\s*)|(\s*$)/g,"")==""){
								insertErr("请输入正确的身份证号码",$this);
								pass[i]=0;
							}else{
								$this.next("span").remove();
								pass[i]=1;
							}
							break;
							
							case "pid":
							reg= new RegExp("^[0-9]{17}[0-9xX]$|^[0-9]{14}[0-9xX]$");
							if(!reg.test($this.val())|| $this.val().replace(/(^\s*)|(\s*$)/g,"")==""){
								insertErr("请输入正确的身份证号码",$this);
								pass[i]=0;
							}else{
								pass[i]=1;
								$.ajax({
	   								type: "post",
	   								url: "/servicer/unique.htm",
	   								cache: false,
	   								data: {type:"person",name:"person_IDC",value:$this.val()},
	   								success: function(msg){
	   									$this.next("span").remove();
	   									pass[i]=msg;
	   									if(msg==0){
											insertErr("该身份证号已被注册",$this);
											$this.focus();
										}else{
											$this.next("span").remove();
										}
	   								}
								});
							}
							break;
							
							case "cid":
							reg= new RegExp("^[0-9]{17}[0-9xX]$|^[0-9]{14}[0-9xX]$");
							if(!reg.test($this.val())|| $this.val().replace(/(^\s*)|(\s*$)/g,"")==""){
								insertErr("请输入正确的身份证号码",$this);
								pass[i]=0;
							}else{
								pass[i]=1;
								$.ajax({
	   								type: "post",
	   								url: "/servicer/unique.htm",
	   								cache: false,
	   								data: {type:"company",name:"arti_IDC",value:$this.val()},
	   								success: function(msg){
	   									$this.next("span").remove();
	   									pass[i]=msg;
	   									if(msg==0){
											insertErr("该身份证号已被注册",$this);
											$this.focus();
										}else{
											$this.next("span").remove();
										}
	   								}
								});
							}
							break;
							
							case "phone":
							var str = $this.val().replace(/(^\s*)|(\s*$)/g,"");
							reg = new RegExp("^(\\d{3,4}-)?(\\d{7,8})(-\\d*)?$|(1[0-9]{10})$");
							if(!reg.test(str)){
								insertErr("请输入有效电话或手机号码",$this);
								pass[i]=0;
							}else{
								$this.next("span").remove();
								pass[i]=1;
							}
							break;
							
							case "cphone":
							var str = $this.val().replace(/(^\s*)|(\s*$)/g,"");
							reg = new RegExp("^(\\d{3,4}-)?(\\d{7,8})(-\\d*)?$|(1[0-9]{10})$");
							if(!reg.test(str)){
								insertErr("请输入有效电话或手机号码",$this);
								pass[i]=0;
							}else{
								pass[i]=1;
								$.ajax({
	   								type: "post",
	   								url: "/servicer/unique.htm",
	   								cache: false,
	   								data: {type:"company",name:"comp_phone",value:str},
	   								success: function(msg){
	   									$this.next("span").remove();
	   									pass[i]=msg;
	   									if(msg==0){
											insertErr("该号码已被注册",$this);
											$this.focus();
										}else{
											$this.next("span").remove();
										}
	   								}
								});
							}
							break;
							
							case "conphone":
							var str = $this.val().replace(/(^\s*)|(\s*$)/g,"");
							reg = new RegExp("^(\\d{3,4}-)?(\\d{7,8})(-\\d*)?$|(1[0-9]{10})$");
							if(!reg.test(str)){
								insertErr("请输入有效电话或手机号码",$this);
								pass[i]=0;
							}else{
								pass[i]=1;
								$.ajax({
	   								type: "post",
	   								url: "/servicer/unique.htm",
	   								cache: false,
	   								data: {type:"company",name:"contact_phone",value:str},
	   								success: function(msg){
	   									$this.next("span").remove();
	   									pass[i]=msg;
	   									if(msg==0){
											insertErr("该号码已被注册",$this);
											$this.focus();
										}else{
											$this.next("span").remove();
										}
	   								}
								});
							}
							break;
							
							case "mobile":
							var s = $this.val().replace(/(^\s*)|(\s*$)/g,"");
							var reg = new RegExp("^(1[0-9]{10})$");
							if(!reg.test(s)){
								insertErr("请输入有效手机号码",$this);
								pass[i]=0;
							}else{
								$this.next("span").remove();
								pass[i]=1;	
							}
							break;
							
							case "pmobile":
							var s = $this.val().replace(/(^\s*)|(\s*$)/g,"");
							var reg = new RegExp("^(1[0-9]{10})$");
							if(!reg.test(s)){
								insertErr("请输入有效手机号码",$this);
								pass[i]=0;
							}else{
								pass[i]=1;
								$.ajax({
	   								type: "post",
	   								url: "/servicer/unique.htm",
	   								cache: false,
	   								data: {type:"person",name:"person_mobile",value:$this.val()},
	   								success: function(msg){
	   									$this.next("span").remove();
	   									pass[i]=msg;
	   									if(msg==0){
											insertErr("该手机号码已被注册",$this);
											$this.focus();
										}else{
											$this.next("span").remove();
										}
	   								}
								});	
							}
							break;
							
							case "num":
							reg = new RegExp("^[0-9]*$");
							if($this.val().replace(/(^\s*)|(\s*$)/g,"")!=""){
								if(!reg.test($this.val())){
									insertErr("请输入数字",$this);
									pass[i]=0;
								}else{
									$this.next("span").remove();
									pass[i]=1;
								}
							}else {
								insertErr("请输入数字",$this);
								pass[i]=0;
							}
							break;
							
							case "Name":
							var s=$this.val().replace(/(^\s*)|(\s*$)/g,"");
							reg=new RegExp("^[A-Za-z\u4e00-\u9fa5.]*$");
							if(s){
								if(reg.test(s)){
									if(s.length>20){
										insertErr("请输入20位以内的中英文和‘.’",$this);	
										pass[i]=0;	
									}else{
										pass[i]=1;	
									}	
								}else{
									insertErr("请输入20位以内的中英文和‘.’",$this);	
									pass[i]=0;	
								}
							}else{
								$err.clone().html("内容不能为空")	.insertAfter($this);
								pass[i]=0;
							}
							break;
							
							case "accountNum":
							var s=$this.val().replace(/(^\s*)|(\s*$)/g,"");
							reg = new RegExp("^[0-9]{10,30}$");
							if(s){
								if(reg.test(s)){
									pass[i]=1;	
								}else{
									insertErr("请输入有效账号",$this);										
									pass[i]=0;		
								}
							}else{
								$err.clone().html("内容不能为空")	.insertAfter($this);
								pass[i]=0;
							}
							break;
							
							case "comName":
							var s=$this.val().replace(/(^\s*)|(\s*$)/g,"");
							if(s){
								s=s.replace(/[0-9]/g,"");
								if(s){
									if(s.length>30){
										insertErr("不能超过30个字符",$this);										
										pass[i]=0;	
									}else{
										pass[i]=1;
										$.ajax({
	   										type: "post",
	   										url: "/servicer/unique.htm",
	   										cache: false,
	   										data: {type:"company",name:"comp_name",value:$this.val()},
	   										success: function(msg){
	   											$this.next("span").remove();
		   										pass[i]=msg;
			   									if(msg==0){
													insertErr("该公司名称已被注册",$this);
													$this.focus();
												}else{
													$this.next("span").remove();
												}
	   										}
										});
									}	
								}else{
									insertErr("不能全为数字",$this);									
									pass[i]=0;	
								}	
							}else{
								insertErr("内容不能为空",$this);										
								pass[i]=0;
							}
							break;
							
							case "username":
							var s=$this.val().replace(/(^\s*)|(\s*$)/g,"");
							var i=0;
							reg=new RegExp("[u4e00-u9fa5]");
							for(var j=0;j<s.length;j++){
								if(!reg.test(s[j])){
									i+=2;
								}else{
									i++;	
								}
							}
							reg=new RegExp("[_]");
							if(!reg.test(s)){
								reg=new RegExp("[A-Za-z0-9\u4e00-\u9fa5]{5,30}$");
								if(!reg.test(s)){
									insertErr("不能输入符号",$this);	
									pass[i]=0;
								}else{
									$this.next("span").remove();
									pass[i]=1;
								}
							}else {
								insertErr("不能输入下划线",$this);	
								pass[i]=0;
							}
							break
					}	
			}
		}
		
		$.fn.tgFileValidation=function(p){
			var $this=$(this);
			var i=getIndex(p,$this);
			var infostr=$this.attr("title");
			var imgV = $("#img"+i);
			$this.focusout(function(){
				$this.next("span").remove();
				if(imgV.val()){
					pass[i]=1;
				}
				function insertErr(str,obj){
					if(infostr){
						$err.clone().html(infostr).insertAfter(obj);
					}else{
						$err.clone().html(str).insertAfter(obj);
					}	
				}
				if($this.val()){
					var file = $this.val();
					var fileType = file.substring(file.lastIndexOf(".")+1).toLowerCase();
     				if(!/(jpg|jpeg|png)$/.test(fileType)){
            			insertErr("图片类型必须是jpeg,jpg,png中的一种",$this);
            			pass[i]=0;
            		}else{
            			$this.next("span").remove();
            			pass[i]=1;
            		}					
				}else if(!imgV.val()&&!$this.val()){				
					insertErr("请上传图片",$this);
					pass[i]=0;
				}
			});
			
		}
		
		$.fn.tgRandCValidation=function(p){
			var $this=$(this);
			var infostr=$this.attr("title");
			var	className;
			var	num;
			if($this.attr("validation")){
				var vStr=$this.attr("validation");
				if(vStr.indexOf("-")>0){
					className = vStr.split('-')[0];
					num = vStr.split('-')[1];
				}
			}
			var name=$this.attr("name");
			var $these=$("input[name='"+name+"']");
			var i=getIndex(p,$this);
			var l=$these.length;
			
			$these.focusout(function(){
				$these.eq(l-1).next("label").next("span").remove();
				if(className){
					$("."+className).removeClass("tips");
	 				$("."+className).text("");
				}
				if($("input[name='"+name+"']:checked").length==0){
					pass[i]=0;
					if(infostr){
						if(className){
							$("."+className).addClass("tips");
							$("."+className).text(infostr);
						}else{
							$err.clone().html(infostr).insertAfter($these.eq(l-1).next("label"));
						}
					}else{
						if(className){
							$("."+className).addClass("tips");
							$("."+className).text("至少选择一项");
						}else{
							$err.clone().html("至少选择一项").insertAfter($these.eq(l-1).next("label"));
						}
					}
				}else if(num){
					if($("input[name='"+name+"']:checked").length>num){
						pass[i]=0;
						if(className){
							$("."+className).addClass("tips");
							$("."+className).text("至多可以选择六项服务种类");
						}else{
							$err.clone().html("至多可以选择六项服务种类").insertAfter($these.eq(l-1).next("label"));
						}
					}
				}else{
					pass[i]=1;
				}
			});	
		}
		
		$.fn.tgSValidation=function(p){
			var $this=$(this);
			var i=getIndex(p,$this);
			var infostr=$this.attr("title");
			if($this.attr("or")){
				var s=$("#"+$this.attr("or")).val().replace(/(^\s*)|(\s*$)/g,"");
			}else{
				var s="";
			}
			$this.change(function(){
				$this.next("span").remove();
				if(!s){
					if($this.find("option").eq(0).is(":selected")){
						pass[i]=0;
						if(infostr){
		  					$err.clone().html(infostr).insertAfter($this);
		  				}else{
		  					$err.clone().html("必选项").insertAfter($this);
		  				}	
					}else{
						pass[i]=1;
					}
				}else{
					pass[i]=1;
				}
			});
		}
		
		$.fn.tgFormValidation=function(){
			var $this=$(this);
			$this.find("input[type='text'][isvali='true'],input[type='hidden'][isvali='true']").each(function(){
				$(this).tgTextValidation($this);
			});
			$this.find("input[type='file'][isvali='true']").each(function(){
				$(this).tgFileValidation($this);
			});
			$this.find("input[type='radio'][isvali='true']").each(function(){
				$(this).tgRandCValidation($this);
			});
			$this.find("input[type='checkbox'][isvali='true']").each(function(){
				$(this).tgRandCValidation($this);
			});
			$this.find("select[isvali='true']").each(function(){
				$(this).tgSValidation($this);
			});
			$this.submit(function(){
				
				pass=new Array();
				$this.find("input[isvali='true']").each(function(){
					$(this).trigger("focusout");
				});
				
				$this.find("select[isvali='true']").each(function(){
					$(this).trigger("change");
				});
				for(i in pass){
					ispass=ispass*pass[i];	
				}
				if(ispass==0){
					for(i in pass){
						if(pass[i]==0){
							if($this.find("*[isvali='true']").eq(i).attr("type")=="hidden"){
								var forstr=$this.find("*[isvali='true']").eq(i).attr("forinput");
								if(forstr){
									$("#"+forstr)[0].focus();
								}else{
									$this.find("*[isvali='true']")[i+1].focus();
								}
							}else{
								$this.find("*[isvali='true']")[i].focus();
							}
							return false;	
						}	
					}
				}else{
				}
			});
			
		}
		function getIndex(p,c){
			var i=p.find("*[isvali='true']").index(c);
			return i;				
		}
		
		/*$(".up_file").click(function(){
			if($(this).val()){
				$(this).next("span.tips").remove();
				$(this).change();
			}
		});*/
		
		$(".up_file").change(function(){
			if($(this).val()){
				$(this).next("span.tips").remove();
			}
			var $this=$(this);
			var index=$("input.up_file").index($this);
			var $pre=$(".sub_preview").eq(index);
			var $img = $("<img />");
			var str="";
			$img.css({"width":"60px","height":"60px"});
			$pre.html("");
			var s=navigator.userAgent.toLowerCase();
			$pre.append($img).parent();	
			if(s.indexOf("msie 6.0")>=0){
				$img[0].src=this.value; 
			}else if(s.indexOf("msie 7.0")>=0 || s.indexOf("msie 8.0")>=0 || s.indexOf("msie 9.0")>=0){
				/*$this[0].select();
				var imgSrc = document.selection.createRange().text;
				$img[0].style.width = "60px";
				$img[0].style.height = "60px";
				$img[0].style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
				$img[0].filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;*/
				$(this).nextAll(".sub_preview").find("img").attr("src",$(this).val());
			}else{
				var oFReader = new FileReader();
				oFReader.readAsDataURL($this[0].files.item(0));
				oFReader.onload = function (oFREvent) { 
					var ofr = oFREvent.target.result; 
					$img.attr("src",ofr);
				}; 
			}
			for(i in $("input.up_file")){
				if($("input.up_file").eq(i).val()){
					str+=$("input.up_file").eq(i).val()+",";
				}				
			}
			$("#imgValue").val(str);
		});
		
})(jQuery);